// plugin/pages/submitOrder/submitOrder.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payWay:1,
    paytypeNote:'微信支付'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    
    console.log(e)
    if(e&&e.id){
      that.setData({
        id:e.id
      })
    } 
    if (e && e.tid) {
      that.setData({
        tid: e.tid
      })
    } 
    if (e && e.type) {
      that.setData({
        type: e.type
      })
    }
    if(that.data.type=='apply'){
      app.setNavtitle('支付保证金');
      that.createOrder();
    }else{
      app.setNavtitle('支付尾款');
      that.payRemain();
    }
  },
  onShow:function(){
    var that = this;
    that.requestpayType();
    wx.getStorage({
      key: 'chooseAddr',
      success: function(res) {
        console.log("获取缓存地址成功")
        that.setData({
          curAddress:res.data
        })
      },
      fail:function(){
        console.log("获取缓存地址失败")
        that.setData({
          curAddress: ''
        })
      }
    })
  },
  onUnload: function () {
    wx.removeStorage({
      key: 'chooseAddr',
      success: function (res) {
        console.log("清除选择地址缓存成功")
      }
    })
  },
  requestpayType: function () {
    var that = this;
    wx.showLoading({
      title: '正在加载',
      mask: true,
      time: 100000
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_pay_cfg'
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          that.setData({
            payType: res.data.data
          })
        } else {
          console.log(res.data)
          that.setData({
            payType: []
          })
          // app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  payRemain: function () {
    var that = this;
    var data = {
      map: 'applet_auction_balance_cfg',
      tid: that.data.tid
    };
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          that.setData({
            orderInfo: res.data.data,
            payMoney: parseFloat(res.data.data.balance),
            hasData: 1
          })
          var leftTime = res.data.data.leftTime;
          that.countDjs(leftTime);
          if (leftTime>0){

          }
        } else {
          console.log(res.data)
          that.setData({
            auctionInfo: res.data.data,
            hasData: 0
          })
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  createOrder:function(){
    var that = this;
    var data = {
      map: 'applet_auction_create_trade',
      id: that.data.id
    };
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          that.setData({
            orderInfo: res.data.data,
            payMoney: parseFloat(res.data.data.money),
            hasData:1
          })
        } else {
          console.log(res.data)
          that.setData({
            auctionInfo: res.data.data,
            hasData: 0
          })
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  confirmPay:function(){
    var that = this;
    if (that.data.orderStatus&&that.data.orderStatus=='dzf') {
      that.orderPay();
    }else{
      that.confirmOrder();
    }
  },
  confirmOrder: function () {
    var that = this;
    var data = {
      map: 'applet_auction_confirm_trade',
      tid: that.data.orderInfo.tid,
      payType: that.data.payWay,
      addrid: that.data.curAddress && that.data.curAddress.id ? that.data.curAddress.id:''
    };
    if (!data.addrid) {
      app.errorTip(that, '请选择收货地址', 2000);
      return;
    }
    console.log(data);
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          that.setData({
            orderStatus: res.data.data.status
          })
          if(data.payType==1){
            that.orderPay();
          }else{
            wx.redirectTo({
              url: '/subpages0/auctionpage/myAuction/myAuction'
            })
          }
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  confirmpayRemain: function () {
    var that = this;
    var data = {
      map: 'applet_auction_pay_balance',
      tid: that.data.tid,
      payType: that.data.payWay
    };
    console.log(data);
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          if (data.payType == 1) {
            that.requestPayment(res.data.data.params, that.data.type, data.tid);
          } else {
            wx.redirectTo({
              url: '/subpages0/auctionpage/orderDetail/orderDetail?tid='+data.tid
            })
          }
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  orderPay: function (e) {
    var that = this;
    var data = {};
    data.map = 'applet_order_pay';
    data.tid = that.data.orderInfo.tid;
    wx.hideLoading();
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          that.requestPayment(res.data.data, that.data.type,data.tid)
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  requestPayment:function(params,type,tid){
    wx.requestPayment({
      'appId': params.appId,
      'timeStamp': params.timeStamp,
      'nonceStr': params.nonceStr,
      'package': params.package,
      'signType': params.signType,
      'paySign': params.paySign,
      'success': function (res) {
        wx.showModal({
          title: '提示',
          content: '支付成功',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.showLoading({
                title: '加载中',
              })
              setTimeout(function () {
                wx.hideLoading();
                if (type == 'apply') {
                  wx.redirectTo({
                    url: '/subpages0/auctionpage/myAuction/myAuction'
                  })
                } else {
                  wx.redirectTo({
                    url: '/subpages0/auctionpage/orderDetail/orderDetail?tid=' + tid
                  })
                }
              }, 1000)
            }
          }
        })
      },
      'fail': function (res) {
        console.log(res);
      }
    });
  },
  choosePaytype:function(e){
    var that = this;
    var dataset = e.currentTarget.dataset;
    var type = dataset.type;
    var typenote = dataset.typenote;
    var disabled = dataset.disabled;
    if (disabled){
      return;
    }
    that.setData({
      payWay:type,
      paytypeNote: typenote
    })
    console.log(type);
  },
  countDjs: function (leftTime) {
    var that = this;
    if (leftTime > 0) {
      var days = parseInt(leftTime / 60 / 60 / 24, 10); //计算剩余的天数 
      var hours = parseInt(leftTime / 60 / 60 % 24, 10); //计算剩余的小时 
      var minutes = parseInt(leftTime / 60 % 60, 10);//计算剩余的分钟 
      var seconds = parseInt(leftTime % 60, 10);//计算剩余的秒数 
      hours = hours < 10 ? '0' + hours : hours;
      minutes = minutes < 10 ? '0' + minutes : minutes;
      seconds = seconds < 10 ? '0' + seconds : seconds;
      that.setData({
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds
      })
      leftTime--;
      if (days == '00' && hours == '00' && minutes == '00' && seconds == '00') {
        clearInterval(that.data.intervarID);
        leftTime = 0;
      }
    } else {
      that.setData({
        days: '00',
        hours: '00',
        minutes: '00',
        seconds: '00',
      })
      clearInterval(that.data.intervarID);
      leftTime = 0;
    }
    that.data.intervarID = setTimeout(function () {
      that.countDjs(leftTime);
    }, 1000)
  },
  toAddressList:function(){
    wx.navigateTo({
      url: '/subpages0/auctionpage/addressList/addressList',
    })
  },
  toRecharge: function () {
    wx.navigateTo({
      url: '/subpages/walletRecharge/walletRecharge',
    })
  },
})